package com.lee.common.adapter.binding

import androidx.databinding.ViewDataBinding
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.lee.common.vo.PageVO


open class SimpleBindingAdapter<T, B : ViewDataBinding>(
    layoutResId: Int,
    data: MutableList<T>,
    private var brId: Int
) : BaseDataBindingAdapter<T, B>(layoutResId, data) {

    var mData: MutableList<PageVO<T>> = arrayListOf()

    override fun convert(binding: B, item: T) {
        binding.setVariable(brId, item)
        itemClickListener?.invoke(binding, item)
        itemPositionClickListener?.invoke(binding, item, getItemPosition(item))
    }

    private var itemClickListener: ((binding: B, item: T) -> Unit)? = null
    private var itemPositionClickListener: ((binding: B, item: T, position: Int) -> Unit)? = null

    private var itemClickChildListener: ((holder: BaseDataBindingHolder<B>) -> Unit)? = null

    open fun addListener(click: (binding: B, itemData: T) -> Unit) {
        itemClickListener = click
    }


    open fun addListener(click: (binding: B, itemData: T, position: Int) -> Unit) {
        itemPositionClickListener = click
    }

    open fun addListener(click: (layoutRes: BaseDataBindingHolder<B>) -> Unit) {
        itemClickChildListener = click
    }

    open fun addAll(list: MutableList<T>, isFirst: Boolean) {
        if (isFirst)
            setNewInstance(list)
        else
            addData(list)
    }

}